package controller;

import com.alibaba.fastjson.JSONObject;
import entity.Gradetable;
import entity.exam;
import org.apache.http.entity.ContentType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import service.ExamService;
import service.GradetableService;
import utils.MultipartFileDto;
import utils.PageObject;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (Gradetable)班级设置对应controller
 *
 * @author makejava
 * @since 2021-12-27 15:14:59
 */
@RestController
@RequestMapping("/gradetable")
public class GradetableController {
    /**
     * 服务对象
     */
    @Resource
    private GradetableService gradetableService;

    @Resource
    private ExamService examService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Gradetable selectOne(Integer id) {
        return this.gradetableService.queryById(id);
    }

    @RequestMapping("/selectbylimit")
    @CrossOrigin
    public PageObject selectallbylimit(@RequestBody JSONObject limitobj){
        PageObject pageObject = gradetableService.queryAllByLimit(limitobj);
        return pageObject;
    }
    @RequestMapping("/insert")
    @CrossOrigin
    public JSONObject insert(@RequestBody JSONObject insertobj){
        JSONObject insert = gradetableService.insert(insertobj);
        return insert;
    }

    @RequestMapping("/delete")
    @CrossOrigin
    public boolean delete(@RequestBody JSONObject insertobj){
        return  gradetableService.deleteById(insertobj.getInteger("pkId"));

    }

    @RequestMapping("/autoFind")
    @CrossOrigin
    public JSONObject autoFind(@RequestBody JSONObject insertobj){
        JSONObject result = new JSONObject();
        Date date1 = insertobj.getDate("date1");
        Date date2 = insertobj.getDate("date2");
        String time  = insertobj.getString("time");
        List<exam> exams = examService.autoFind(date1, date2, time);
        result.put("data", exams);
        return  result;

    }


    @RequestMapping("/importExcel")
    public JSONObject importExcel(@RequestBody JSONObject fileobj) throws Exception {
//    public JSONObject importExcel(@RequestParam MultipartFile file){
//        JSONObject object=new JSONObject();
//        try{
//
//        }catch (Exception e){
//            object.put("code","-1");
//            object.put("msg",e.toString());
//        }
        System.out.println("========="+fileobj.toString());
        String file = fileobj.getString("file");
        System.out.println(file);
        File excelfile=new File(file);
        FileInputStream fileInputStream=new FileInputStream(file);
        MultipartFile multipartFile=new MultipartFileDto(excelfile.getName(),excelfile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
        JSONObject object = gradetableService.importtoExcel(multipartFile);
        System.out.println("============="+object.toString());
        return object;
    }
    @PostMapping("/import2")
    public boolean addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = gradetableService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  a;
    }
}
